package net.androy.hoola;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class Horoscope extends ListActivity {

    List<HoroscopeItem> horoscopeItems = new ArrayList<HoroscopeItem>();
    private String category = "";

    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

	Bundle bundle = getIntent().getExtras();
	category = (String) bundle.get("category");

	setProgressBarIndeterminate(true);
	setProgressBarIndeterminateVisibility(true);

	getListView().setClickable(false);
    }

    @Override
    protected void onStart() {
	super.onStart();
	
	setProgressBarIndeterminateVisibility(true);
	final Handler handler = new Handler();
	Runnable r = new Runnable() {
	    public void run() {
		// retrieve the feed
		FeedRetriever.getFeed(horoscopeItems, category);

		handler.post(new Runnable() {
		    public void run() {
			Horoscope.this.setListAdapter(new CustomAdapter(Horoscope.this, horoscopeItems));
			Horoscope.this.getListView().setTextFilterEnabled(true);
			Horoscope.this.setProgressBarIndeterminateVisibility(false);
		    }
		});
	    }
	};

	new Thread(r).start();
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
	((CustomAdapter) getListAdapter()).toggle(position);
    }

    private class CustomAdapter extends BaseAdapter {
	private List<HoroscopeItem> items;
	private Context ctx;
	private Boolean[] mExpanded = null;

	public CustomAdapter(Context context, List<HoroscopeItem> items) {
	    this.items = items;
	    List<Boolean> e = new ArrayList<Boolean>();
	    for (int i = 0; i < items.size(); i++) {
		e.add(false);
	    }

	    mExpanded = e.toArray(new Boolean[0]);

	    ctx = context;
	}

	public void toggle(int position) {
	    mExpanded[position] = !mExpanded[position];
	    notifyDataSetChanged();
	}

	public int getCount() {
	    return items.size();
	}

	public Object getItem(int position) {
	    return items.get(position);
	}

	public long getItemId(int position) {
	    // TODO Auto-generated method stub
	    return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
	    HoroscopeView sv;
	    if (convertView == null) {
		sv = new HoroscopeView(ctx, items.get(position).getTitle(), items.get(position).getDescription(),
			mExpanded[position]);
	    } else {
		sv = (HoroscopeView) convertView;
		sv.setTitle(items.get(position).getTitle());
		sv.setDialogue(items.get(position).getDescription());
		sv.setExpanded(mExpanded[position]);
	    }

	    return sv;
	}

    }

    private class HoroscopeView extends LinearLayout {
	public HoroscopeView(Context context, String title, String dialogue, boolean expanded) {
	    super(context);

	    this.setOrientation(VERTICAL);

	    mTitle = new TextView(context);
	    mTitle.setText(title);
	    mTitle.setTextSize(14);
	    mTitle.setTextColor(Color.WHITE);

	    addView(mTitle, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 40));
	    mDialogue = new TextView(context);
	    mDialogue.setText(dialogue);
	    addView(mDialogue, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

	    mDialogue.setVisibility(expanded ? VISIBLE : GONE);
	}

	/**
	 * Convenience method to set the title of a SpeechView
	 */
	public void setTitle(String title) {
	    mTitle.setText(title);
	}

	/**
	 * Convenience method to set the dialogue of a SpeechView
	 */
	public void setDialogue(String words) {
	    mDialogue.setText(words);
	}

	/**
	 * Convenience method to expand or hide the dialogue
	 */
	public void setExpanded(boolean expanded) {
	    mDialogue.setVisibility(expanded ? VISIBLE : GONE);
	}

	private TextView mTitle;
	private TextView mDialogue;
    }

}
